﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.BLL.Statistics;
using MINERP.Statistics.IView;
using System.Data;
using MINERP.MODEL.FoundationModel;

namespace MINERP.Statistics.Presenter
{
    public class PPerformance
    {

        private IFrmPerformance _iView;
        private PerformanceBLL _bll;
        public PPerformance(IFrmPerformance inView)
        {
            _iView = inView;
            _bll = new PerformanceBLL();
            //_iView.StartTime = Convert.ToDateTime("2000-01-01");
            //_iView.EndTime = DateTime.Now;
        }
        public void Init()
        {
            _iView.BandingGroup(_bll.GetAllGroup());
        }
        public DataSet GetPerformance()
        {
            // List<PerformanceView> listView = _bll.GetPerformanceAll(_iView.GroupId,_iView.StartTime,_iView.EndTime);
            List<PerformanceView> listView;
            if (_iView.GroupName == "全部")
            {
                listView = _bll.GetPerformanceAll(_iView.StartTime, _iView.EndTime);

            }
            else
            {
                if (_iView.UserName == "全部")
                {
                    listView = _bll.GetPerformanceAll(_iView.GroupName, _iView.StartTime, _iView.EndTime);
                }
                else
                {
                    listView = _bll.GetPerformanceAll(_iView.GroupName, _iView.UserName, _iView.StartTime, _iView.EndTime);
                }
            }

            DataTable viewTable = CreateViewTable();
            foreach (PerformanceView view in listView)
            {
                DataRow row = viewTable.NewRow();
                row["Departments"] = view.Departments;
                row["UserName"] = view.UserName;
                row["Tech"] = view.Tech;
                row["OrderNum"] = view.OrderNum;
                row["PhotoNum"] = view.PhotoNum;
                row["Areas"] = Math.Round( view.Areas,4);
                row["CompletedTime"] = view.CompletedDate;
                row["OutReturnAreas"] = view.OutReturnAreas;
                //row["PaiBanWaste"] = view.PaiBanWaste;
                viewTable.Rows.Add(row);

            }
            viewTable.TableName = "Performance";
            DataSet set = new DataSet();
            set.Tables.Add(viewTable);
            set.WriteXml(@"C:\windows\xmlData\Performance.xml", XmlWriteMode.WriteSchema);


            return set;


        }

        DataTable CreateViewTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Departments", typeof(string));
            table.Columns.Add("UserName", typeof(string));
            table.Columns.Add("Tech", typeof(string));
            table.Columns.Add("OrderNum", typeof(int));
            table.Columns.Add("PhotoNum", typeof(int));
            table.Columns.Add("Areas", typeof(double));
            table.Columns.Add("PaiBanWaste", typeof(double));
            table.Columns.Add("CompletedTime", typeof(DateTime));
            table.Columns.Add("OutReturnAreas", typeof(double));
            return table;
        }

        internal void BindWorkerList(int GroupId)
        {
            List<string> userList = new List<string>();
            if (GroupId > 0)
            {
                userList = _bll.GetUserListByGroupId(GroupId);
            }
            _iView.BindUser(userList);
        }
    }
}
